Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Smart holder] Support void pointer capsules #3633

Merged
merged 15 commits into from
Jan 25, 2022

Conversation

wangxf123456
Copy link
Contributor

@wangxf123456 wangxf123456 commented Jan 21, 2022

Description

Make smart holder type casters support void pointer capsules.

The original motivation for this is to support passing SWIG objects to pybind11 wrapped functions. This is achieved by checking whether the object has a particular method defined. Suppose we have the following C++ code:

namespace pybind11_tests {
namespace void_ptr_capsule {
class Obj { };

void func(const Obj* obj) { }
}
}

Then if an object has the method as_pybind11_tests_void_ptr_capsule_Obj defined, and the method returns a capsule with name pybind11_tests::void_ptr_capsule::Obj, then we think this object can be converted to pybind11_tests::void_ptr_capsule::Obj, so we accept the object as a parameter.

@wangxf123456 wangxf123456 changed the title Smart holder [Smart holder] Support void pointer capsules Jan 21, 2022
@wangxf123456 wangxf123456 marked this pull request as ready for review January 24, 2022 22:05
include/pybind11/detail/smart_holder_type_casters.h Outdated Show resolved Hide resolved
include/pybind11/detail/smart_holder_type_casters.h Outdated Show resolved Hide resolved
include/pybind11/detail/smart_holder_type_casters.h Outdated Show resolved Hide resolved
tests/test_class_sh_void_ptr_capsule.cpp Outdated Show resolved Hide resolved
tests/test_class_sh_void_ptr_capsule.cpp Outdated Show resolved Hide resolved
Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — We're running global testing overnight to be on the safe side.

// Convert `a::b::c` to `a_b_c`
replace_all(type_name, "::", '_');

std::string as_void_ptr_function_name = "as_" + type_name;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very tiny nit, this could be an allocation of "as_" and += type_name and remove an allocation.

@rwgk
Copy link
Collaborator

rwgk commented Jan 25, 2022

This PR was globally tested. Merging now. (Skipping the CI rerun awaiting approval: very low risk.)

@rwgk rwgk merged commit da058a2 into pybind:smart_holder Jan 25, 2022
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Jan 25, 2022
@rwgk rwgk removed the needs changelog Possibly needs a changelog entry label Jan 25, 2022
@wangxf123456 wangxf123456 deleted the smart_holder branch January 25, 2022 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants